* This uses the fourth wave of the 2008 NAES, available upon request: https://www.annenbergpublicpolicycenter.org/data-access/

ssc install todate

********************************************************************************
* Coding
********************************************************************************

* Date
gen date = DATE_4
tostring date, gen(datevar)
todate datevar , p(yyyymmdd) gen(newdate)
sum newdate
drop date datevar
rename newdate date
replace date = date-17840
gen d = date
drop date
rename d date  // number of days before election

* Candidate feeling thermometers
gen obama= ABo02_4
gen mccain = AAm01_4
replace mccain = . if mccain>100
replace obama = . if obama>100

* Party 
gen party = . 
replace party = 1 if  MA01_4 < 4
replace party = 0 if  MA01_4 > 4
label define partylabel 0 "Democrat" 1 "Republican"
label values party partylabel

* In- and out-party feeling thermometers
gen out_party = obama if party==1
replace out_party = mccain if party==0
gen in_party = obama if party==0
replace in_party = mccain if party==1

* Candidate affective polarization
gen affect = in_party-out_party



* region
gen region =  .
replace region = 2 if WFc01_a==	1	// Alabama (AL)
replace region = 4 if WFc01_a==	2	// Alaska (AK)
replace region = 4 if WFc01_a==	4	// Arizona (AZ)
replace region = 2 if WFc01_a==	5	// Arkansas (AR)
replace region = 4 if WFc01_a==	6	// California (CA)
replace region = 4 if WFc01_a==	8	// Colorado (CO)
replace region = 1 if WFc01_a==	9	// Connecticut (CT)
replace region = 2 if WFc01_a==	10	// Delaware (DE)
replace region = 2 if WFc01_a==	11	// District of Columbia (DC)
replace region = 2 if WFc01_a==	12	// Florida (FL)
replace region = 2 if WFc01_a==	13	// Georgia (GA)
replace region = 4 if WFc01_a==	15	// Hawaii (HI)
replace region = 4 if WFc01_a==	16	// Idaho (ID)
replace region = 3 if WFc01_a==	17	// Illinois (IL)
replace region = 3 if WFc01_a==	18	// Indiana (IN)
replace region = 3 if WFc01_a==	19	// Iowa (IA)
replace region = 3 if WFc01_a==	20	// Kansas (KS)
replace region = 2 if WFc01_a==	21	// Kentucky (KY)
replace region = 2 if WFc01_a==	22	// Louisiana (LA)
replace region = 1 if WFc01_a==	23	// Maine (ME)
replace region = 2 if WFc01_a==	24	// Maryland (MD)
replace region = 1 if WFc01_a==	25	// Massachusetts (MA)
replace region = 3 if WFc01_a==	26	// Michigan (MI)
replace region = 3 if WFc01_a==	27	// Minnesota (MN)
replace region = 2 if WFc01_a==	28	// Mississippi (MS)
replace region = 3 if WFc01_a==	29	// Missouri (MO)
replace region = 4 if WFc01_a==	30	// Montana (MT)
replace region = 3 if WFc01_a==	31	// Nebraska (NE)
replace region = 4 if WFc01_a==	32	// Nevada (NV)
replace region = 1 if WFc01_a==	33	// New Hampshire (NH)
replace region = 1 if WFc01_a==	34	// New Jersey (NJ)
replace region = 4 if WFc01_a==	35	// New Mexico (NM)
replace region = 1 if WFc01_a==	36	// New York (NY)
replace region = 2 if WFc01_a==	37	// North Carolina (NC)
replace region = 3 if WFc01_a==	38	// North Dakota (ND)
replace region = 3 if WFc01_a==	39	// Ohio (OH)
replace region = 2 if WFc01_a==	40	// Oklahoma (OK)
replace region = 4 if WFc01_a==	41	// Oregon (OR)
replace region = 1 if WFc01_a==	42	// Pennsylvania (PA)
replace region = 1 if WFc01_a==	44	// Rhode Island (RI)
replace region = 2 if WFc01_a==	45	// South Carolina (SC)
replace region = 3 if WFc01_a==	46	// South Dakota (SD)
replace region = 2 if WFc01_a==	47	// Tennessee (TN)
replace region = 2 if WFc01_a==	48	// Texas (TX)
replace region = 4 if WFc01_a==	49	// Utah (UT)
replace region = 1 if WFc01_a==	50	// Vermont (VT)
replace region = 2 if WFc01_a==	51	// Virginia (VA)
replace region = 4 if WFc01_a==	53	// Washington (WA)
replace region = 2 if WFc01_a==	54	// West Virginia (WV)
replace region = 3 if WFc01_a==	55	// Wisconsin (WI)
replace region = 4 if WFc01_a==	56	// Wyoming (WY)

* Participation
gen rally = KH10rA_b if KH10rA_b <100
revrs rally , replace
replace rally = rally-1

gen contact = KH10rB_b if KH10rB_b<100
revrs contact , replace
replace contact = contact-1

gen campaign = KH10rC_b if KH10rC_b<100
revrs campaign , replace
replace campaign = campaign-1

gen donate = KH10rD_b if KH10rD_b<100
revrs donate , replace
replace donate = donate-1

gen community = KH10rE_b if KH10rE_b<100
revrs community , replace
replace community = community-1

gen letter = KH10rG_b if KH10rG_b<100
revrs letter , replace
replace letter = letter-1

gen involv = rally+contact+campaign+donate+community+letter

gen race = WC01_a
recode race (3=4) (4=3) (5=4)

* Is respondent interviewer pre-convention?
gen preconvention =0
replace preconvention = 1 if date<=-61


********************************************************************************
* Models
********************************************************************************
* Main model (Model 1 in Table 1)
reg affect date
* Limits to conventions (Model 2 in Table 1)
reg affect date if date>-61 // after conventions are over

* In and out party (Models 3 and 4 in Table 2)
reg in_party date if date>-61
reg out_party date if date>-61


* Robustness checks:
* Adjusts for pre-convention interivew
reg affect date preconvention
reg in_party date preconvention
reg out_party date preconvention

* Balance test and histogram of survey dates
cd
cd desktop
log using "naes balance" , text replace
reg  date WA05_a WA03_a WA02_a involv i.race i.WFa08_a i.region
log close

gen d = date
# delimit ;
	hist d 
	,
	fcolor(gs12) 
	disc 
	aspect(1) 
	freq 
	ytitle(Count) 
	ylab(0(200)800 1000 "1,000",angle(0)) 
	xlab(-60 "60" -40 "40" -20 "20" 0 "0") 
	xtitle(Days before election)
	;
# delimit cr


* Main models with covariates
cd 
cd desktop
reg affect date WA05_a WA03_a WA02_a involv i.race i.WFa08_a i.region
	outreg2 using naes_covariates.tex, replace ctitle(All ) parent(se) dec(3) alpha(.05) symbol(*) 
reg affect date WA05_a WA03_a WA02_a involv i.race i.WFa08_a i.region if date>-61
	outreg2 using naes_covariates.tex, append ctitle(post) parent(se) dec(3) alpha(.05) symbol(*) 

* squared model:
reg affect c.date##c.date
	outreg2 using naes.tex, replace ctitle(All observations no_cov) parent(se) dec(3) alpha(.05) symbol(*) 
reg affect c.date##c.date WA05_a WA03_a WA02_a involv i.race i.WFa08_a i.region
	outreg2 using naes.tex, append ctitle("All") parent(se) dec(3) alpha(.05) symbol(*) 
reg affect c.date##c.date if date>-61
	outreg2 using naes.tex, append ctitle(post no_cov) parent(se) dec(3) alpha(.05) symbol(*) 
reg affect c.date##c.date WA05_a WA03_a WA02_a involv i.race i.WFa08_a i.region if date>-61
	outreg2 using naes.tex, append ctitle(post w_cov) parent(se) dec(3) alpha(.05) symbol(*) 



* Logged days
cd
cd desktop
log using "logged days" , text
reg affect lndate
	outreg2 using naes.tex, replace ctitle(All ) parent(se) dec(3) alpha(.05) symbol(*) 
reg affect lndate WA05_a WA03_a WA02_a involv i.race i.WFa08_a i.region
	outreg2 using naes.tex, append ctitle(All 2 ) parent(se) dec(3) alpha(.05) symbol(*) 
reg affect lndate if date>-61
	outreg2 using naes.tex, append ctitle(post) parent(se) dec(3) alpha(.05) symbol(*) 
reg affect lndate WA05_a WA03_a WA02_a involv i.race i.WFa08_a i.region if date>-61
	outreg2 using naes.tex, append ctitle(post 2) parent(se) dec(3) alpha(.05) symbol(*) 


* In and out party full range of dates
reg in_party date 
	outreg2 using naes.tex, replace ctitle(In-party ) parent(se) dec(3) alpha(.05) symbol(*) 
reg out_party date  
	outreg2 using naes.tex, append ctitle(Out-party ) parent(se) dec(3) alpha(.05) symbol(*) 

* In and out party with covariates
reg in_party date WA05_a WA03_a WA02_a involv i.race i.WFa08_a i.re if date>-61
	outreg2 using naes_covariates.tex, append ctitle(In-party ) parent(se) dec(3) alpha(.05) symbol(*) 
reg out_party date WA05_a WA03_a WA02_a involv i.race i.WFa08_a i.re if date>-61
	outreg2 using naes_covariates.tex, append ctitle(Out-party ) parent(se) dec(3) alpha(.05) symbol(*) 

* Squared in and out party models
reg in_party c.date##c.date if date>-61
	outreg2 using naes.tex, replace ctitle(In-party ) parent(se) dec(3) alpha(.05) symbol(*) 
reg in_party c.date##c.date WA05_a WA03_a WA02_a involv i.race i.WFa08_a i.re if date>-61
	outreg2 using naes.tex, append ctitle(In-party ) parent(se) dec(3) alpha(.05) symbol(*) 
reg out_party c.date##c.date if date>-61
	outreg2 using naes.tex, append ctitle(In-party ) parent(se) dec(3) alpha(.05) symbol(*) 
reg out_party c.date##c.date WA05_a WA03_a WA02_a involv i.race i.WFa08_a i.re if date>-61
	outreg2 using naes.tex, append ctitle(In-party ) parent(se) dec(3) alpha(.05) symbol(*) 

* Test  
gen op_recode = out_party
revrs op_recode , replace 
	replace op_recode = op_recode - 1

# delimit ;
sureg 
	(in_party c.date if date>-61)
	(op_recode c.date if date>-61)
	;
# delimit cr

test _b[op_recode:date]-_b[in_party:date]=0


* output for R
# delimit ;
	keep 
	affect
	in_party
	out_party
	date
	WA05_a
	WA03_a
	WA02_a
	involv 
	race
	WFa08_a
	region
	;
# delimit cr 

drop if affect == .
drop if date == .
drop if WA05_a == .
drop if WA03_a == .
drop if WA02_a == .
drop if involv == . 
drop if race == .
drop if WFa08_a == .
drop if region == .
drop if in_party == .
drop if out_party == .

saveold "/Users/jrthornton/Desktop/naes_for_R.dta", version(12)
